package com.jdone.compus.controller;

import com.jdone.compus.dto.ApiResponse;
import com.jdone.compus.model.BlowDeviceRepair;
import com.jdone.compus.service.BlowDeviceRepairService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/blow/repair")
@Api(value = "吹风机维修上报", tags = "吹风机维修上报")
public class BlowDeviceRepairController {

    @Autowired
    private BlowDeviceRepairService blowDeviceRepairService;

    @ApiOperation("上报维修")
    @PostMapping("/wechat/add")
    public ApiResponse addRepairRecord(@RequestParam Long deviceId,
                                       @RequestParam String repairName,
                                       @RequestParam String repairAccount,
                                       @RequestParam String repairType,
                                       @RequestParam String repairPhone,
                                       @RequestParam String repairNotes,
                                       @RequestParam(required = false) String images) {
        return ApiResponse.success(blowDeviceRepairService.addRepairRecord(deviceId, repairName, repairAccount, repairType, repairPhone, repairNotes, images));
    }

    @ApiOperation("分页查询维修记录")
    @GetMapping("/end_back/findAll")
    public ApiResponse getAllRepairRecords(@RequestParam int page, @RequestParam int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        Page<BlowDeviceRepair> repairRecordsPage = blowDeviceRepairService.getAllRepairRecords(pageable);
        return ApiResponse.success(repairRecordsPage);
    }

    @ApiOperation("修改维修记录")
    @PutMapping("/end_back/update/{repairId}")
    public ApiResponse updateRepairRecord(@PathVariable Long repairId) {
        try {
            BlowDeviceRepair updatedRepair = blowDeviceRepairService.updateRepairRecord(repairId);
            return ApiResponse.success(updatedRepair);
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }

    @ApiOperation("删除维修记录")
    @DeleteMapping("/end_back/delete/{repairId}")
    public ApiResponse deleteRepairRecord(@PathVariable Long repairId) {
        try {
            blowDeviceRepairService.deleteRepairRecord(repairId);
            return ApiResponse.success("维修记录删除成功");
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }

}

